import 'package:book_app/product/localstore/local_storage.dart';
import 'package:book_app/product/models/userInfo.dart';
import 'package:flutter/material.dart';
import 'package:mobx/mobx.dart';

import '../../../product/base/base_view_model.dart';
import '../../../product/dio_http/achttp.dart';
import '../../main/main_page.dart';
import '../service/login_service.dart';

class LoginViewModel with Store, BaseViewModel, ChangeNotifier {
  @override
  void setContext(BuildContext context) => viewModelContext = context;

  @override
  void init() {}
  bool isLoading = false;
  late String username;
  late String password;
  late UserInfo userInfo;
  final LoginService _loginService = LoginService();

  Future<bool> login() async {
    isLoading = true;
    bool isLogin = false;
    notifyListeners();
    try {
      userInfo = await _loginService.login(username, password);
      print("跳转首页${userInfo.toString()}");
      isLogin = true;
      print("跳转首页${viewModelContext.mounted}");
      if(viewModelContext.mounted){
        Navigator.push(
          viewModelContext,
          MaterialPageRoute(
            builder: (context) => const MainPage(),
          ),
        );
      }
    } catch (e) {
      isLogin = false;
    }
    isLoading = false;
    notifyListeners();
    return isLogin;
  }
}
